#!/usr/bin/perl
use Getopt::Std;
#getopt("d:g:r",\%args);
getopt("d:t",\%args);
my $dir = $args{d};
my $temp_dir = $args{t};
#my $geno_file = $args{g};
#my $refsamp = $args{r};
chomp($dir);
#chomp($geno_file);
#chomp($refsamp); 
if($dir eq "")
{
        die "entered dir name where subfolders are created is empty\n";
}
if($temp_dir eq "")
{
        die "entered tempdir name where subfolders are created is empty\n";
}

#if($geno_file eq "")
#{
#	die "entered genotype file name is empty\n";
#}
#if($refsamp eq "")
#{
#	die "entered refsamp population name empty";
#}	

#$dir = '/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_vte/clean/beagle/';
#$geno_file ='/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_vte/clean/beagle/processed_beagle_input_vte';
$geno_file = "processed_beagle_input";
$refsamp = '"ASN|ASW|CEU|TSI|YRI"';
#$i=21;
open(WRB,">$temp_dir/submitjobs");
#chopping each chr and submitting the beagle imputation jobs
for($i=0;$i<22;$i++)
{
	$j = $i+1;
	system("mkdir $temp_dir/$j");
        $tempdir = "$temp_dir/$j";
	#print "$geno_file\t$j\t$refsamp\t$tempdir\n";	
	$sys = "sh $dir/bin/runbeagle_ind.csh $temp_dir/$geno_file $j $refsamp $tempdir $dir/bin";
	print $sys."\n";	
	system($sys);
	$geno = "geno.bgl";
	$marker = "markers.txt";
	$reference = "phased.bgl";
	$sys = "/usr/bin/perl $dir/bin/perl_chop_beagle_files_new.pl -s $tempdir/$geno -m $tempdir/$marker -r $tempdir/$reference -w 5000000 -e 250000 -d $tempdir -t $temp_dir -k $dir> $tempdir/perl_chop_beagle_files.log";
	#print "processing chopping chr $j \n$sys\n";
	system($sys);
}
